let nums = [5, 7, 7, 8, 8, 10];
let target = 8;

let bound = (nums, target, flags) => {
    let left = 0;
    let right = nums.length - 1;
    while (left <= right) {
        let mid = Math.floor((left + right) / 2);
        if (flags === 1) {
            if (target <= nums[mid]) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        } else if (flags === 2) {
            if (target >= nums[mid]) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
    }
    if (flags === 1) return left;
    else if (flags === 2) return right;
}

let binarySearch = (nums, target) => {
    let low = bound(nums, target, 1);
    let high = bound(nums, target, 2);
    if (low > high) {
        return [-1, -1];
    }
    return [low, high];
}
console.log(binarySearch(nums, target))